package app.android.com.wuandroidapp.test.view.definedview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * Created by Administrator on 2017/7/31.
 */

public class MyTextViewLinarGradient extends TextView {
    Paint paint1,paint2;
    public MyTextViewLinarGradient(Context context) {
        super(context);
//        paint1=new Paint();
//        paint1.setColor(getResources().getColor(android.R.color.holo_blue_light));
//        paint1.setStyle(Paint.Style.FILL);
//        paint2=new Paint();
//        paint2.setColor(Color.YELLOW);
//        paint2.setStyle(Paint.Style.FILL);

    }

    public MyTextViewLinarGradient(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
       if(mGradientMatrix!=null){
           mTranslate+=mViewWidth/5;
           if(mTranslate>2*mViewWidth){
               mTranslate=-mViewWidth;
           }
           mGradientMatrix.setTranslate(mTranslate,0);
           mLinearGradient.setLocalMatrix(mGradientMatrix);
           postInvalidateDelayed(100);
       }
        super.onDraw(canvas);
  //      canvas.restore();
    }
    int mViewWidth=0;
  int mTranslate;
    LinearGradient mLinearGradient;
    Matrix mGradientMatrix;
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if(mViewWidth==0){
           mViewWidth=getMeasuredWidth();
            if(mViewWidth>0){
                paint1=getPaint();
                mLinearGradient=new LinearGradient(0,0,mViewWidth,0,new int[]{Color.BLUE,0xffffffff,Color.RED},null, Shader.TileMode.CLAMP);
                paint1.setShader(mLinearGradient);
                mGradientMatrix=new Matrix();

            }
        }
    }
}
